import { defineStore } from 'pinia'
import { ref } from 'vue';
export const useSystemStore = defineStore('system',
    () => {
        const tagsList = ref([])

        // 主题背景数据
        const backgroundList = {
            dark: {
                hBgColor:"#242f42",
                hTextColor:"#fff",
                sBgColor:"#324157",
                sTextColor:"#bfcbd9",
                sActiveTextColor:"#20a0ff",
                contentBgColor:"#DCDFE6"
            },
            light:{
                hBgColor:"#f3d19e",
                hTextColor:"#909399",
                sBgColor:"#faecd8",
                sTextColor:"#909399",
                sActiveTextColor:"#f89898",
                contentBgColor:"#fdf6ec"
            }
        }

        // 控制侧边栏状态   
        const collapse = ref(false)

        // 控制主题背景
        const switchBg = ref(JSON.parse(localStorage.getItem('bgColor')) ||backgroundList.dark )

        const homeItem = ref([{
            name: 'DashBoard',
            path: '/dashboard',
            title: '系统首页'
        }])
        
        // 修改主题背景
        const cahngeBackground = (data) => {
            switchBg.value = backgroundList[data]
            localStorage.setItem('bgColor',JSON.stringify(switchBg.value))
        }
            // 删除标签页
           const delTagsItem =(data)  => {
            tagsList.value.splice(data.index, 1)
            }
            // 新增标签页
            const setTagsItem =(data) => {
                tagsList.value.push(data)
            }
            // 关闭所有（首页不可关闭）
           const clearTags = () => {
                // state.tagsList = []
                tagsList.value = [...homeItem.value]
            }
            // 关闭其他（首页不可关闭）
            const closeTagsOther = (data) =>  {
                tagsList.value = [...homeItem.value]
                if (data[0].path !== homeItem.value[0].path) {
                    tagsList.value.push(data[0])
                }
            }
            // 关闭当前标签页
            const closeCurrentTag = (data) =>  {
                for (let i = 0, len = tagsList.value.length; i < len; i++) {
                    const item = tagsList.value[i];
                    if (item.path === data.$route.fullPath) {
                        if (i < len - 1) {
                            data
                                .$router
                                .push(state.tagsList[i + 1].path);
                        } else if (i > 0) {
                            data
                                .$router
                                .push(state.tagsList[i - 1].path);
                        } else {
                            data
                                .$router
                                .push('/');
                        }
                        state
                            .tagsList
                            .splice(i, 1);
                        break;
                    }
                }
            }
            // 侧边栏折叠
           const hadndleCollapse =(data) => {
                collapse.value = data;
                // sessionStorage['collapse'] = data;
            }
            return {
                tagsList,
                collapse,
                homeItem,
                switchBg,
                delTagsItem,
                setTagsItem,
                clearTags,
                closeTagsOther,
                closeCurrentTag,
                hadndleCollapse,
                cahngeBackground
            }

    }

  

)